package gb;

import gb.struct.Monomial;
import gb.struct.Polynomial;

public class Utils {
    
    public static Monomial negatePow(Monomial m) {
        Monomial result = new Monomial(1 / m.getCoefficient());
        
        for(int i = 0; i < m.getSize(); i++) {
            result.addPower(m.getPower(i)*-1);
        }
        
        return result;
    }
    
    public static Monomial negateCoef(Monomial m) {
        Monomial result = (Monomial) m.clone();
        result.setCoefficient(result.getCoefficient() * -1);
        return result;
    }

    public static Monomial lcm(Monomial m1, Monomial m2) {
        Monomial result = new Monomial(lcm((long)m1.getCoefficient(), (long)m2.getCoefficient()));
        
        for (int i = 0; i<Monomial.SIZE; i++) {
            result.addPower((int)lcm(m1.getPower(i), m2.getPower(i)));
        }
        
        return result;
    }

    
    public static long lcm(long a, long b)
    {
        if (a != 0 && b!= 0) {
            return a * (b / gcd(a, b));
        } else if (a != 0){
            return a;
        } else {
            return b;
        }
    }
    
    public static long gcd(long a, long b)
    {
        while (b > 0)
        {
            long temp = b;
            b = a % b; // % is remainder
            a = temp;
        }
        return a;
    }
    
    public static Polynomial normalForm(Polynomial p, PolySet set) {
        Reductor red = new Reductor();
        red.setSet(set);
        Polynomial result = null;
        
        while (p != null) {
            result = (Polynomial) p.clone();
            result.sort();
            p = red.reduce(result);
        }
        
        return result;
    }
    
    public static Monomial getLM(Polynomial p)
    {
    	Monomial result = (Monomial) p.getMono(0).clone();
    	result.setCoefficient((double) 1);
    	return result;
    }

}
